/*
 *This AI is an easy choose a random available move AI
 *blue = white = 0
 *green = black = 1
 */
import java.util.*;
 
public class MayoPlayer extends Player
{
	String color;
	int pieces;
	int player;
	int playerSpace;
	int otherPlayerSpace;
	int boardSize;
	//image
    
    public MayoPlayer(int _boardSize, int p)
    {
    	super(p, 'm');
    	pieces = 0;
    	boardSize = _boardSize;
		player = p;
		if(player == 0)
		{
			color = "green";
			playerSpace = -1;
			otherPlayerSpace = 1;
		} else
		{
			color = "blue";
			playerSpace = 1;
			otherPlayerSpace = -1;
		}	
    }
    
    /*
     *BoardState -1 = black, 1 = white, 0 = empty
     */
    public void makeMove(int boardSize, int[][] boardState, int[] lastMove, Long maxTime, int[] newMove)
    {
		List moves = new ArrayList(10);
		//find all valid moves -- threading possibility
		for (int i = 0; i < boardSize; i++)
		{
			for (int j = 0; j < boardSize; j++)
			{
				if (isValid(boardState, i, j))
				{
					int[] move;
					move = new int[2];
					move[0] = i;
					move[1] = j;
					moves.add(move);
				}
			}
		}
		//printing all valid moves for testing
		//System.out.println("CPU's valid moves are:");
		for (int i = 0; i < moves.size(); i++)
		{
			int[] aMove;
			aMove = (int[])moves.get(i);
			//System.out.print("x: ");
			//System.out.print(aMove[0]);
			//System.out.print(" y: ");
			//System.out.println(aMove[1]);
		}
		//choose a random valid move
		Random generator = new Random();
		if (moves.size() == 0) //returning an invalid move will result in a pass
		{
			newMove[0] = 0;
			newMove[1] = -1;
		}
		else if (moves.size() > 1)
		{
			int randomIndex = generator.nextInt(moves.size()-1);
			int[] someMove;
			someMove = (int[])moves.get(randomIndex);
			newMove[0] = someMove[0];
			newMove[1] = someMove[1];
			//System.out.print("CPU making move x: " );
			//System.out.print(newMove[0]);
			//System.out.print(" y: ");
			//System.out.println(newMove[1]);
		}
		else 
		{
			newMove[0] = ((int[])moves.get(0))[0];
			newMove[1] = ((int[])moves.get(0))[1];
		}
    }
    
    public void makeBadMove(int boardSize, int[][] boardState, int[] lastMove, Long maxTime, int[] newMove)
    {
    	makeMove(boardSize, boardState, lastMove, maxTime, newMove);
    }
    
    private boolean emptySpace(int[][] boardState, int x, int y)
    {
    	if (boardState[x][y] == 0)
    		return true;
    	return false;
    }
    private boolean nextToOpponentPiece(int[][] boardState, int x, int y)
    {
    	if (x != boardSize-1)
    		if (boardState[x+1][y] == otherPlayerSpace)
    			return true;
    	if (x != 0)
    		if (boardState[x-1][y] == otherPlayerSpace)
    			return true;
    	if (y != boardSize-1)
    		if (boardState[x][y+1] == otherPlayerSpace)
    			return true;
    	if (y != 0)
    		if (boardState[x][y-1] == otherPlayerSpace)
    			return true;
    	if ((x != boardSize-1) && (y != boardSize-1))
    		if (boardState[x+1][y+1] == otherPlayerSpace)
    			return true;
    	if ((x != boardSize-1) && (y != 0))
    		if (boardState[x+1][y-1] == otherPlayerSpace)
    			return true;
    	if ((x != 0) && (y != boardSize-1))
    		if (boardState[x-1][y+1] == otherPlayerSpace)
    			return true;
    	if ((x != 0) && (y != 0))
    		if (boardState[x-1][y-1] == otherPlayerSpace)
    			return true;
    	return false;
    } //possibly obsolete
    private boolean captureable(int[][] boardState, int x, int y)
    {
    	//count up
    	int i = x;
    	int j = y-1;
    	if (j >= 0)
    		if (boardState[i][j] == otherPlayerSpace)
    			while (j >= 0)
    			{
    				if (boardState[i][j] == 0)
    					break;
    				if (boardState[i][j] == playerSpace)
    				{
    					return true;
    				}
    				j--;
    			}
    	
    	//count down
    	i = x;
    	j = y+1;
    	if (j < boardSize)
    		if (boardState[i][j] == otherPlayerSpace)
    			while ((j < boardSize))
    			{
    				if (boardState[i][j] == 0)
    					break;
    				if (boardState[i][j] == playerSpace)
    					return true;
    				j++;
    			}
    	//count right
    	i = x+1;
    	j = y;
    	if (i < boardSize)
    		if (boardState[i][j] == otherPlayerSpace)
    			while ((i < boardSize))
    			{
    				if (boardState[i][j] == 0)
    					break;
    				if (boardState[i][j] == playerSpace)
    					return true;
    				i++;
    			}
    	//count left
    	i = x-1;
    	j = y;
    	if (i >= 0)
    		if (boardState[i][j] == otherPlayerSpace)
    			while (i >= 0)
    			{
    				if (boardState[i][j] == 0)
    					break;
    				if (boardState[i][j] == playerSpace)
    					return true;
    				i--;
    			}
    	//count up-right
    	i = x+1;
    	j = y-1;
    	if ((i < boardSize) && (j >= 0))
    		if (boardState[i][j] == otherPlayerSpace)
    			while ((i < boardSize) && (j >= 0))
    			{
    				if (boardState[i][j] == 0)
    					break;
    				if (boardState[i][j] == playerSpace)
    					return true;
    				i++;
    				j--;
    			}
    	//count up-left
    	i = x-1;
    	j = y-1;
    	if ((i >= 0) && (j >= 0))
    		if (boardState[i][j] == otherPlayerSpace)
    			while ((i >= 0) && (j >= 0))
    			{
    				if (boardState[i][j] == 0)
    					break;
    				if (boardState[i][j] == playerSpace)
    					return true;
    				i--;
    				j--;
    			}
    	//count down-right
    	i = x+1;
    	j = y+1;
    	if ((i < boardSize) && (j < boardSize))
    		if (boardState[i][j] == otherPlayerSpace)
    			while ((i < boardSize) && (j < boardSize))
    			{
    				if (boardState[i][j] == 0)
    					break;
    				if (boardState[i][j] == playerSpace)
    					return true;
    				i++;
    				j++;
    			}
    	//count down-left;
    	i = x-1;
    	j = y+1;
    	if ((i >= 0) && j < boardSize)
    		if (boardState[i][j] == otherPlayerSpace)
    			while ((i >= 0) && (j < boardSize))
    			{
    				if (boardState[i][j] == 0)
    					break;
    				if (boardState[i][j] == playerSpace)
    					return true;
    				i--;
    				j++;
    			}
    	return false;
    }
    
    private boolean isValid(int[][] boardState, int x, int y)
    {
    	if (emptySpace(boardState, x, y))
    	{
    		//System.out.println("found an empty space");
    		if (nextToOpponentPiece(boardState, x, y))
    		{
    			//System.out.println("found a space next to opponent");
    			if (captureable(boardState, x, y))
    			{
    				//System.out.println("found a capturable space");
    				return true;
    			}
    		}
    	}
    	return false;
    }
}